package com.sservice.web.controller.pc;

import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.commonBusiness.e.Comment;
import com.sservice.commonBusiness.e.DataR;
import com.sservice.commonBusiness.service.CommentService;
import com.sservice.commonBusiness.service.DataRService;
import com.sservice.commonBusiness.vo.CommentVo;
import com.sservice.model.entity.Info;
import com.sservice.model.entity.UserWx;
import com.sservice.model.vo.InfoVo;
import com.sservice.service.service.IInfoService;
import com.sservice.service.service.IUserWxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/info")
public class InfoPcController {
    @Autowired
    CommentService commentService;
    @Autowired
    IUserWxService iUserWxService;
    @Autowired
    DataRService dataRService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private IInfoService service;
    @Autowired
    private IUserWxService userWxService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Integer> create(Info arg, String skey, HttpSession session) {
        if (arg.getSource() != null && arg.getSource().startsWith("points:")) { //发布问题，扣减积分
            int points = Integer.valueOf(arg.getSource().replace("points:", ""));
            arg.setSource(String.valueOf(points));
            UserWx salep = userWxService.getLogined(skey);
            salep = userWxService.selectByPrimaryKey(salep.getId());
//            salep.setPointsNow(salep.getPointsNow().subtract(points));
            userWxService.updateByPrimaryKeySelective(salep);// 扣减积分
        }
        int service_res = service.insertSelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/bestAnswer", method = RequestMethod.POST)
    public BaseResult<Integer> bestAnswer(Comment arg, String skey) {
        Comment comment = commentService.selectByPrimaryKey(arg.getId());
        String userId = comment.getUserId();
        UserWx userWx = userWxService.selectByPrimaryKey(Integer.valueOf(userId));
        Info info = service.selectByPrimaryKey(Integer.valueOf(comment.getTargetId()));
        int points = Integer.valueOf(info.getSource());
//        userWx.setPointsNow(userWx.getPointsNow() + points);
//        userWx.setPoints(userWx.getPoints() + points);
        userWxService.updateByPrimaryKeySelective(userWx);
        BaseResult<Integer> res = new BaseResult(true, commentService.updateByPrimaryKeySelective(arg));
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public BaseResult detail(Info arg) {
        Info info = service.selectByPrimaryKey(arg.getId());
        if ("USER".equals(arg.getVideo())) {//
            UserWx userWx = iUserWxService.selectByPrimaryKey(Integer.valueOf(info.getAuthorId()));
            InfoVo infoVo = new InfoVo();
            infoVo.setInfo(info);
            infoVo.setUserWx(userWx);
            return new BaseResult(true, infoVo);

        }

        BaseResult res = new BaseResult(true, info);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/comment/count/{id}", method = RequestMethod.POST)
    public BaseResult<Integer> commentCount(Info arg) {
        CommentVo commentVo = new CommentVo();
        commentVo.setTargetId(String.valueOf(arg.getId()));
        commentVo.setType1((arg.getType1()));
        arg.setRead(commentService.pageCount(commentVo));
        BaseResult<Integer> res = new BaseResult(true, service.updateByPrimaryKeySelective(arg));
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/favor/count/{id}/{authorId}", method = RequestMethod.POST)
    public BaseResult<Integer> favorCount(Info arg) {
        DataR commentVo = new DataR();
        commentVo.setSourceId((arg.getId()));//
        commentVo.setType1(arg.getType1());//favor
        commentVo.setTargetId(Integer.valueOf(arg.getAuthorId()));
        arg.setFavor(dataRService.countBy(commentVo));
        BaseResult<Integer> res = new BaseResult(true, service.updateByPrimaryKeySelective(arg));
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(Info arg) {
        BaseResult<Object> res = null;
        int service_res = service.deleteByPrimaryKey(arg.getId());
        res = new BaseResult(true, service_res);
        return res;
    }


    /***
     * 纯内容的搜素
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryPlain", method = RequestMethod.GET)
    public ListResult query(InfoVo arg) {
        List<Info> list = service.queryByPage(arg);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(arg.getPage(), service.queryCount(arg), arg.getSize()));
        return res;
    }

    /**
     * 好友植物动态
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/friendsTrend", method = RequestMethod.GET)
    public ListResult friendsTrend(InfoVo arg) {
        List<Info> list = service.queryByPage(arg);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(arg.getPage(), service.queryCount(arg), arg.getSize()));
        return res;
    }

    /**
     * 包含分类的搜索
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryWithCategory", method = RequestMethod.GET)
    public ListResult queryWithCategory(InfoVo arg) {
        List<InfoVo> list = service.queryWithCategoryByPage(arg);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(arg.getPage(), service.queryCount(arg), arg.getSize()));
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/queryWithDr", method = RequestMethod.GET)
    public ListResult queryWithDr(InfoVo arg) {
        List<Info> list = service.queryWithDrByPage(arg);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(arg.getPage(), service.countWithDrByPage(arg), arg.getSize()));
        return res;
    }


}
